<?php
class DeviceModel extends model
{
	static $_instance = null;

	static function getInstance ()
	{
		if (is_null(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	// 取得派工设备(总件数)
	function getCount($p){
		$sql  = "select count(*) as c from crm_device where 1=1 ";
	    if($p["name"] != ""){$sql .= "and name like '%".$p["name"]."%'";}
		if($p["types"] != ""){$sql .= "and types = '".$p["types"]."'";}
		if($p["brand"] != ""){$sql .= "and brand = '".$p["brand"]."'";}
		if($p["pattern"] != ""){$sql .= "and pattern='".$p["pattern"]."'";}

		$return = $this->sql($sql)->s(1);
		return $return["c"];
	}
	
	// 取得派工设备(列表)
	function getDevice($p)
	{
		$sql = "select * from crm_device where 1=1 ";
		if($p["name"] != ""){$sql .= "and name like '%".$p["name"]."%'";}
	    if($p["types"] != ""){$sql .= "and types = '".$p["types"]."'";}
		if($p["brand"] != ""){$sql .= "and brand = '".$p["brand"]."'";}
		if($p["pattern"] != ""){$sql .= "and pattern='".$p["pattern"]."'";}
		$sql .= " order by ".$p["sortField"]." ".$p["sortOrder"];
		$sql .= " limit [start],[end]";
		
		return $this->sql($sql,array("start"=>$p["r"]*$p["p"],"end"=>$p["r"]))->s();
	}
	
	// 工单配件记录
	function businessDeviceCount($p){
		$sql = "select count(*) as c "; 
		$sql.= "from crm_device as cd,crm_business_device as cbd,crm_business as cb "; 
		$sql.= "where cbd.business_id = cb.id and cbd.device_id=cd.id and cb.id = [cid]"; 
		$rs = $this->sql($sql,$p)->s(1);
		return $rs["c"];
	}
	
	// 工单配件记录
	function businessDeviceList($p){
		$sql = "select cd.id,cd.id as did,cb.id as cid,cd.name,cs.name as staff,cbd.types,cbd.device_no as no,cd.brand,cd.pattern,cbd.count,cbd.remark ";  
        $sql.= "from crm_device as cd,crm_business as cb,crm_business_device as cbd left join crm_staff as cs on cbd.staff_id=cs.id ";
        $sql.= "where cbd.business_id = cb.id and cbd.device_id=cd.id and cb.id = [cid] "; 
		return $this->sql($sql,$p)->s();
	}
	
	// 工单配件编辑
	function businessDeviceEdit($p){
		$rs = $this->sql("select * from crm_device where id=[device] ",$p)->s(1);
		if(isset($rs["id"]) && $rs["id"]>0){
			return $this->sql("insert into crm_business_device(`staff_id`,`business_id`,`device_no`,`device_id`,`types`,`count`,`remark`)values('[staff]','[id]','[no]',[device],'[types]',[count],'[remark]')",$p)->i();
		}
		
		return false;
	}
	
	// 取得派工设备(删除)
	function DeviceDel($p)
	{
		$this->sql("delete from crm_device where id = [id]",$p)->d();
	}
	
	// 取得派工设备(修改)
	function DeviceUpd($p){
		return $this->sql("update crm_device set `name`='[name]',`types`='[types]',`brand`='[brand]',`pattern`='[pattern]',`remark`='[remark]' where id=[id]",$p)->d();
	}
	
	// 取得派工设备(增加)
	function DeviceAdd($p){
		return $this->sql("insert into crm_device (`name`,`types`,`brand`,`pattern`,`price`,`sum`,`remark`,`created`)values('[name]','[types]','[brand]','[pattern]','[remark]',now())",$p)->i();
	}
	
	// 取得类型下的品牌
	function DeviceBrand($p){
		return $this->sql("select brand as name from crm_device where types='[types]' group by brand ",$p)->s();
	}
	
	// 取得类型\品牌下的型号
	function DevicePattern($p){
		return $this->sql("select pattern as name from crm_device where types='[types]' and brand='[brand]' group by brand ",$p)->s();
	}
}